package com.lulin.mall.product.service.impl;

import com.lulin.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.Query;

import com.lulin.mall.product.dao.BrandDao;
import com.lulin.mall.product.entity.BrandEntity;
import com.lulin.mall.product.service.BrandService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 更新品牌和类目的信息
     *
     * @param brand
     */
    @Override
    public void updateDetail(BrandEntity brand) {
        //1、更新原始数据
        this.updateById(brand);

        //2、获取品牌名称，如果不为空，就根据品牌id去更新关联表中的品牌名称
        if (!StringUtils.isEmpty(brand.getName())) {
            //同步更新类目和品牌关系的数据
            categoryBrandRelationService.updateBrandName(brand.getBrandId(), brand.getName());
            //TODO 同步更新其他的品牌名称的冗余数据
        }
    }

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //1、获取key值
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            //查询条件对比
            wrapper.eq("brand_id", key).or().like("name", key);
        }
        //分页查询
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params), wrapper
                //new QueryWrapper<BrandEntity>()
        );
        return new PageUtils(page);
    }


}
